<?php
/**
 * 登录模型类
 * Created by PhpStorm.
 * User: xiangpeng
 * Date: 2016/12/25
 * Time: 13:29
 */

namespace Common\Model;


class UserModel extends BaseModel
{
    //主键
    protected $pk ='id';
    //要操作的数据库名称
    protected $tableName='user';
    //自动验证
    protected $_validate=[
        ['account','require','账户名不能为空',1,3],
        ['passwd','require','密码不能为空',1,3],
    ];

    //获得验证码
    public function isLogin(){
        //1获得页面提交的验证码
        $data=I('post.');
        //var_dump($res);die;
        if(!$this->check_verify($data['code'])){
            $this->error = '验证码错误';
            return false;
        }
        //2.验证用户名是否存在
       $passwd=md5($data['passwd']);
        $dataModel= $this->where("account='{$data['account']}'")->where("passwd='{$passwd}'")->find();
        //dd($dataModel);DIE;
        if (!$dataModel){
           $this->error = '用户名或密码错误';
           return false;
        }

        //说明数据已经的得到了
        //存入ssession
        session('id',$dataModel['uid']);
        session('uname',$dataModel['uname']);
        //将时间和登录ip及登录状态写入数据库
        //时间
        $data['time']=time();
        //ips
        $data['uip']=$_SERVER['REMOTE_ADDR'];
        $data['passwd']= md5($data['passwd']);
        //执行添加
        //dd($data);die;
        $this->where("uid={$dataModel['uid']}")->save($data);
        //记得要返回结果
        return true;
    }

    // 检测输入的验证码是否正确，$code为用户输入的验证码字符串
    protected function check_verify($code, $id = ''){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

    //public function ip(){
    //    //获取客户IP
    //    $ip=$_SERVER['REMOTE_ADDR'];       //获取客户IP
    //    return $ip;
    //}
}
